package cn.csc.module.distribute.controller.app.distributor;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.security.core.util.SecurityFrameworkUtils;
import cn.csc.module.distribute.controller.app.distributor.vo.AppDistributeInfoRespVO;
import cn.csc.module.distribute.controller.app.distributor.vo.AppDistributorCreateReqVO;
import cn.csc.module.distribute.controller.app.distributor.vo.AppDistributorGetByChildUserIdRespVO;
import cn.csc.module.distribute.controller.app.distributor.vo.AppDistributorReqVO;
import cn.csc.module.distribute.controller.app.distributor.vo.AppDistributorRespVO;
import cn.csc.module.distribute.convert.distributor.DistributorConvert;
import cn.csc.module.distribute.dal.dataobject.distributor.DistributorDO;
import cn.csc.module.distribute.service.distributor.DistributorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigInteger;

import static cn.csc.framework.common.pojo.CommonResult.success;

/**
 * @author liuchuang
 * @version 1.0
 * @modify
 * @since 2025/4/22 11:07
 */
@Api(tags = "APP用户 - 分销员信息")
@RestController
@RequestMapping("/distribute/distributor")
@Validated
public class AppDistributorController {

    @Resource
    private DistributorService distributorService;


    /**
     * 申请分销员
     */
    @PostMapping("/apply")
    @ApiOperation("申请分销员")
    public CommonResult<BigInteger> createDistributor(@Valid @RequestBody AppDistributorCreateReqVO createReqVO) {
        return success(distributorService.createDistributor(createReqVO));
    }

    /**
     * 查询当前用户的推广信息
     */
    @GetMapping("/distributeInfo")
    @ApiOperation("查询当前用户的推广信息")
    public CommonResult<AppDistributeInfoRespVO> getDistributeInfo() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return CommonResult.success(distributorService.getDistributeInfo(BigInteger.valueOf(userId)));
    }

    /**
     * 查询当前用户的推广二维码
     */
    @GetMapping("/distributeWxCode")
    @ApiOperation("查询当前用户的推广二维码")
    public CommonResult<String> getDistributeWxCode() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return CommonResult.success(distributorService.getDistributeWxCode(BigInteger.valueOf(userId)));
    }

    /**
     * 获取当前用户所属的分销员
     */
    @GetMapping("getByCurrentUserId")
    @ApiOperation("通过下线用户ID查询分销员")
    public CommonResult<AppDistributorGetByChildUserIdRespVO> getByChildUserId(){
        DistributorDO byChildUserId = distributorService.getByChildUserId(BigInteger.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        return success(DistributorConvert.INSTANCE.convert2(byChildUserId));
    }


    /**
     * 我的推广,佣金信息
     */
    @PostMapping("/getDistributorCommission")
    @ApiOperation("我的推广,佣金信息")
    public CommonResult<AppDistributorRespVO> getDistributorByUserId(AppDistributorReqVO reqVO) {
        return success(DistributorConvert.INSTANCE.convert1(distributorService.getDistributorById(reqVO)));
    }

}
